package nc.impl.hrhi.bill;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import nc.bs.dao.BaseDAO;
import nc.bs.hrhi.bp.BatchFileDownBp;
import nc.bs.hrhi.bp.BatchFileUploadBp;
import nc.bs.hrhi.bp.BuluFileTypeBp;
import nc.bs.hrhi.bp.FileRecordBp;
import nc.bs.hrhi.bp.FileTypeBp;
import nc.bs.hrhi.bp.LoginInfoBp;
import nc.bs.hrhi.bp.QueryLeaderBp;
import nc.bs.hrhi.bp.QueryNewTreeDataBp;
import nc.bs.hrhi.bp.QueryQysDefaultInfoBp;
import nc.bs.hrhi.bp.QysBp;
import nc.bs.hrhi.bp.QysLxOperBp;
import nc.bs.hrhi.util.PumaUtil;
import nc.hr.utils.PubEnv;
import nc.itf.hrhi.bill.IXcHrExtBillService;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.pub.tools.HiSQLHelper;
import nc.vo.hi.psndoc.CertVO;
import nc.vo.hi.psndoc.CtrtVO;
import nc.vo.pub.BusinessException;
import nc.vo.util.SqlWhereUtil;
import nccloud.commons.lang.StringUtils;

public class XcHrExtBillServiceImpl implements IXcHrExtBillService {

	@Override
	public void sendPuma(String funcode, String funname, String content, String[] usercodes) throws BusinessException {
		PumaUtil.send(funcode, funname, content, usercodes);
	}

	@Override
	public Map<String, Map<String, String>> queryLeaderByPsnPk(String[] pk_psndocs) throws BusinessException {
		return new QueryLeaderBp().exec(pk_psndocs);
	}

	@Override
	public CertVO[] queryShenfenzhengData(Vector v) throws BusinessException {
		
		String sql = "select b.begindate,\r\n" + 
				"b.creationtime,\r\n" + 
				"b.creator,\r\n" + 
				"b.dr,\r\n" + 
				"e.glbdef26 as enddate,\r\n" + 
				"b.extend_org,\r\n" + 
				"b.id,\r\n" + 
				"b.idtype,\r\n" + 
				"b.iseffect,\r\n" + 
				"b.isstart,\r\n" + 
				"b.lastflag,\r\n" + 
				"b.memo,\r\n" + 
				"b.modifiedtime,\r\n" + 
				"b.modifier,\r\n" + 
				"b.pk_group,\r\n" + 
				"b.pk_org,\r\n" + 
				"b.pk_psndoc,\r\n" + 
				"b.pk_psndoc_sub,\r\n" + 
				"b.recordnum,\r\n" + 
				"b.ts\r\n" + 
				"from bd_psndoc e inner join hi_psndoc_cert b on b.pk_psndoc=e.pk_psndoc where b.dr=0 and b.iseffect='Y' and b.lastflag='Y'";
		
		sql = sql + " and EXISTS (\r\n" + 
				"  select 1 from (\r\n" + 
				" select top 1 hi_psnjob.trnsevent from hi_psnjob where hi_psnjob.lastflag='Y' and hi_psnjob.recordnum=0 and hi_psnjob.pk_psndoc=e.pk_psndoc order by hi_psnjob.begindate desc\r\n" + 
				" ) temp where temp.trnsevent <> 4\r\n" + 
				" )";
		
		sql = sql + "  and e.glbdef26 >= '" + v.elementAt(0).toString().trim() + "' and e.glbdef26 <= '"
				+ v.elementAt(1).toString().trim() + "'";

		ArrayList<CertVO[]> al = (ArrayList) new BaseDAO().executeQuery(sql, new BeanListProcessor(CertVO.class));

		if ((al == null) || (al.size() == 0)) {
			return null;
		}
		
		CertVO[] result = new CertVO[al.size()];
		al.toArray(result);

		return result;
	}
	
	private String getPowerSql(String userid, String strSQL, String alias) {
		SqlWhereUtil sqlWhereUtil = new SqlWhereUtil(strSQL);

		String orgSql = HiSQLHelper.getPsnPowerSql(PubEnv.getPk_group(), "60050orginfo", "default", alias);
		if (StringUtils.isNotBlank(orgSql)) {
			sqlWhereUtil.and(orgSql);
		}

		String deptSql = HiSQLHelper.getPsnPowerSql(PubEnv.getPk_group(), "60050deptinfo", "default", alias);
		if (StringUtils.isNotBlank(deptSql)) {
			sqlWhereUtil.and(deptSql);
		}

		String psnclSql = HiSQLHelper.getPsnPowerSql(PubEnv.getPk_group(), "psncl", "default", alias);
		if (StringUtils.isNotBlank(psnclSql)) {
			sqlWhereUtil.and(psnclSql);
		}
		return sqlWhereUtil.getSQLWhere();
	}

	@Override
	public Map<String, Map<String, String>> queryQysDefaultInfoByPsnPk(String[] pk_psndocs) throws BusinessException {
		return new QueryQysDefaultInfoBp().exec(pk_psndocs);
	}

	@Override
	public void qysLxOper(String pk_qyschoose, String[] pk_psndocs)
			throws BusinessException {
		new QysLxOperBp().exec(pk_qyschoose, pk_psndocs);
	}

	public void batchFileUpload(Map<String, InputStream> filemap) throws BusinessException {
		new BatchFileUploadBp().exec(filemap);
	}

	@Override
	public List<Map<String, Object>> queryNewTreeData(String[] pk_psndocs) throws BusinessException {
		return new QueryNewTreeDataBp().exec(pk_psndocs);
	}

	@Override
	public File batchFileDown(Map<String, String[]> webMap) throws BusinessException {
		return new BatchFileDownBp().exec(webMap);
	}

	@Override
	public void fileRecord(String pk_doc) throws BusinessException {
		new FileRecordBp().exec(pk_doc);
	}

	@Override
	public List<String> queryAllFileType() throws BusinessException {
		return new FileTypeBp().execAll();
	}

	@Override
	public List<String> queryFileTypeByUserPower(String cuserid) throws BusinessException {
		return new FileTypeBp().execByUserPower(cuserid);
	}

	@Override
	public Map<String, String> getLoginPsnInfo() throws BusinessException {
		return new LoginInfoBp().get();
	}

	@Override
	public void buluFileType(String pk_psndoc) throws BusinessException {
		new BuluFileTypeBp().exec(pk_psndoc);
	}

	@Override
	public boolean isPsnFile(String path20) throws BusinessException {
		return new FileRecordBp().isPsnFileByPath20(path20);
	}

	@Override
	public void qysEdit__RequiresNew(CtrtVO vo) throws BusinessException {
		new QysBp().execEdit(vo);
	}

}
